package com.lovo.studentmanagement;


import javax.swing.*;
import java.util.Scanner;

/**
 * 学生管理主界面
 */
public class StudentUI {
//    Scanner scanner = new Scanner(System.in);
    SuperArray superArray = new SuperArray();
    /**
     * 菜单
     */
    public void menu(){
        String index;
        while (true){
//            System.out.println("\n1-查找学生 2-添加学生 3-删除学生 4-修改学生 5-退出");
//            index = scanner.nextInt();
            index = JOptionPane.showInputDialog(null,"1-查找学生 2-添加学生 3-删除学生 4-修改学生 5-退出");
            if (index == null){
                JOptionPane.showMessageDialog(null,"程序已退出");
                System.exit(0);
            }
            switch (index){
                case "1":
                    lookup();
                    break;
                case "2":
                    add();
                    break;
                case "3":
                    del();
                    break;
                case "4":
                    change();
                    break;
                case "5":
                    System.exit(0);
                default:
                    JOptionPane.showMessageDialog(null,"输入出错");
                    continue;
            }
        }
    }


    /**
     * 添加学生
     */
    public void add(){
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        if (name == null){
            return;
        }
        int age = 0;
        try {
            age = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄："));
        }catch (Exception e){
            JOptionPane.showMessageDialog(null,"年龄输入出错");
            return;
        }

//        System.out.println("请输入学生性别：");
        String gender = JOptionPane.showInputDialog(null,"请输入学生性别：");
        if (gender == null){
            return;
        }
        Student student = new Student(name, age, gender);

        superArray.add(student);
    }

    /**
     * 删除学生
     */
    public void del(){
//        System.out.println("请输入学生姓名");
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        if (name == null){
            return;
        }
        for (int i = 0; i < superArray.size(); i++){
            Student student =  (Student) superArray.get(i);
            if (student.getName().equals(name)){
                int confirm = JOptionPane.showConfirmDialog(null,"确认是否删除");
                if (confirm == 0){
                    superArray.remove(i);
                    JOptionPane.showMessageDialog(null,"未查找到相关学生");
                    return;
                }else {
                    return;
                }

            }

        }
        JOptionPane.showMessageDialog(null,"未查找到相关学生");


    }

    /**
     * 修改学生
     */
    public void change(){
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        if (name == null){
            return;
        }
        for (int i = 0; i < superArray.size(); i++){
            Student student =  (Student) superArray.get(i);
            if (student.getName().equals(name)){
                String newName = "";
                newName = JOptionPane.showInputDialog(null,"请输入学生姓名：");
                if (newName == null){
                    return;
                }
                try {
                    student.setAge(Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄：")));
                }catch (Exception e){
                    JOptionPane.showMessageDialog(null,"年龄输入出错");
                    return;
                }
                String newGender = "";
                newGender = JOptionPane.showInputDialog(null,"请输入学生性别：");
                if (newGender == null){
                    return;
                }
                JOptionPane.showMessageDialog(null,"修改成功");
                return;

            }

        }
        JOptionPane.showMessageDialog(null,"未查找到相关学生");
    }

    /**
     * 查找学生
     */
    public void lookup(){
        String students = "";
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名（如要查询所有学生请输入1）：");
        if (name.equals("1")){
            for (int i = 0; i < superArray.size(); i++){
                Student student =  (Student) superArray.get(i);
                students = students +
                        "姓名：" + student.getName() +
                        "，年龄：" + student.getAge() +
                        "，性别：" + student.getGender() +
                        "\n";
            }
            JOptionPane.showMessageDialog(null,students);
        }else {
            for (int i = 0; i < superArray.size(); i++){
                Student student = (Student) superArray.get(i);
                if (student.getName().equals(name)){
                    students = students +
                            "姓名：" + student.getName() +
                            "，年龄：" + student.getAge() +
                            "，性别：" + student.getGender();
                    JOptionPane.showMessageDialog(null,students);
                    return;
                }

            }
            JOptionPane.showMessageDialog(null,"未查找到相关学生");
        }


    }

    public static void main(String[] args) {
        StudentUI studentUI = new StudentUI();
        studentUI.menu();
    }

}


